package DataStructure.tree;

/**
 * 617. 合并二叉树 https://leetcode.cn/problems/merge-two-binary-trees/
 * 题目简述：将两棵二叉树重叠合并，重叠节点合并节点值，否则直接作为新二叉树的节点
 */
public class MergeTrees {

    /**
     * 思路：递归。合并子树root1和root2 等于 合并根节点 并 合并其左子树以及合并其右子树
     */
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        root1.val = root1.val + root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        return root1;
    }
}
